package com.book.remote;

import com.book.dto.AddressDto;
import com.book.helper.AddressHelper;
import com.book.pojo.Address;
import com.book.service.AddressService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class AddressRemote {
    @Autowired
    private AddressService addressService;

    public List<AddressDto> getList(String uCode) {
        List<AddressDto> list = addressService.getList(uCode);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list;
    }

    public AddressDto getAddressById(Integer id) {
        Address addressById = addressService.getAddressById(id);
        return addressById == null ? null : AddressHelper.covertPoDto(addressById);
    }

    public Boolean editAddress(AddressDto addressDto) {
        try {
            log.info("AddressRemote.editAddress#addressDto:{}",addressDto);
            return addressService.updateAddress(addressDto);
        }catch (Exception e){
            return false;
        }
    }

    public Boolean addAddress(AddressDto addressDto) {
        try {
            Integer integer = addressService.addAddr(addressDto);
            return integer==1;
        }catch (Exception e){
         return false;
        }
    }

    public Boolean delete(Integer id) {
        return addressService.delete(id);
    }
}
